{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from csv import DictReader\n",
    "import numpy as np\n",
    "\n",
    "from sklearn import svm\n",
    "from sklearn.model_selection import StratifiedShuffleSplit\n",
    "from sklearn.model_selection import GridSearchCV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_csv = 'layer_features/adc/features_train_combined.csv'\n",
    "test_csv = 'layer_features/adc/features_test_conbined.csv'\n",
    "n_estimators = 400\n",
    "\n",
    "def to1hot(zone):\n",
    "    zones = ['AS', 'PZ', 'SV', 'TZ']\n",
    "    \n",
    "    result = [float(zone == e) for e in zones]\n",
    "    return result\n",
    "    \n",
    "\n",
    "train_data = []\n",
    "train_labels = []\n",
    "train_proxIds = []\n",
    "\n",
    "with open(train_csv) as csvfile:\n",
    "    reader = DictReader(csvfile)\n",
    "    fields = list(reader.fieldnames)\n",
    "    fields.remove('proxid')\n",
    "    fields.remove('clinsig')\n",
    "    fields.remove('Age') # because convert to float.\n",
    "    fields.remove('Zone') # categorical data -> 1-hot\n",
    "    \n",
    "    for row in reader:\n",
    "        train_proxIds.append(row.pop('proxid'))\n",
    "        train_labels.append(row.pop('clinsig'))\n",
    "        data_item= []\n",
    "        for field in fields:\n",
    "            data_item.append(row[field])\n",
    "        data_item.append(float(row['Age'][:-1]) / 10)\n",
    "        data_item.extend(to1hot(row['Zone']))\n",
    "        train_data.append(data_item)\n",
    "        \n",
    "\n",
    "test_data = []\n",
    "test_proxIds = []\n",
    "\n",
    "with open(test_csv) as csvfile:\n",
    "    reader = DictReader(csvfile)\n",
    "    fields = list(reader.fieldnames)\n",
    "    fields.remove('proxid')\n",
    "    fields.remove('Age') # because convert to float.\n",
    "    fields.remove('Zone') # categorical data -> 1-hot\n",
    "    \n",
    "    for row in reader:\n",
    "        test_proxIds.append(row.pop('proxid'))\n",
    "        data_item= []\n",
    "        for field in fields:\n",
    "            data_item.append(row[field])\n",
    "        data_item.append(float(row['Age'][:-1]) /10)\n",
    "        data_item.extend(to1hot(row['Zone']))\n",
    "        test_data.append(data_item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The best parameters are {'gamma': 0.0025514065200312819, 'C': 15.361749466718233} with a score of 0.81\n"
     ]
    }
   ],
   "source": [
    "C_range = np.logspace(-30, 10, 60)\n",
    "gamma_range = np.logspace(-30, 3, 60)\n",
    "param_grid = dict(gamma=gamma_range, C=C_range)\n",
    "cv = StratifiedShuffleSplit(n_splits=5, test_size=0.3, random_state=42)\n",
    "grid = GridSearchCV(svm.SVC(), param_grid=param_grid, cv=cv)\n",
    "grid.fit(train_data, train_labels)\n",
    "\n",
    "print(\"The best parameters are %s with a score of %0.2f\"\n",
    "      % (grid.best_params_, grid.best_score_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SVC(C=15.361749466718233, cache_size=200, class_weight=None, coef0=0.0,\n",
       "  decision_function_shape=None, degree=3, gamma=0.002551406520031282,\n",
       "  kernel='rbf', max_iter=-1, probability=True, random_state=None,\n",
       "  shrinking=True, tol=0.001, verbose=False)"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf = svm.SVC(C=15.361749466718233, cache_size=200, class_weight=None, coef0=0.0,\n",
    "  decision_function_shape=None, degree=3, gamma=0.0025514065200312819, kernel='rbf',\n",
    "  max_iter=-1, probability=True, random_state=None, shrinking=True,\n",
    "  tol=0.001, verbose=False)\n",
    "clf.fit(train_data, train_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "test_result = clf.predict_proba(test_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0472838316313\n",
      "0.686111105926\n",
      "0.473850391351\n",
      "0.315857051573\n",
      "0.293350257313\n",
      "0.193653706784\n",
      "0.197896988001\n",
      "0.0829525371187\n",
      "0.170920314184\n",
      "0.819208105615\n",
      "0.31026051972\n",
      "0.552360280576\n",
      "0.709872253483\n",
      "0.474453934374\n",
      "0.135731461496\n",
      "0.228956716304\n",
      "0.106783962354\n",
      "0.207747272972\n",
      "0.141715787483\n",
      "0.373314203772\n",
      "0.399101333891\n",
      "0.210459434322\n",
      "0.515958154716\n",
      "0.24961359885\n",
      "0.590902764745\n",
      "0.0568319291234\n",
      "0.13998813396\n",
      "0.0612279022516\n",
      "0.138228424388\n",
      "0.805681848187\n",
      "0.194449293767\n",
      "0.224453548269\n",
      "0.115210474502\n",
      "0.143762588261\n",
      "0.142230021831\n",
      "0.20502662687\n",
      "0.879932584288\n",
      "0.894386792042\n",
      "0.494423852399\n",
      "0.0545571022866\n",
      "0.093493842585\n",
      "0.187935124898\n",
      "0.274152490912\n",
      "0.409428455482\n",
      "0.291369589023\n",
      "0.0994192600996\n",
      "0.095067065525\n",
      "0.0576115091341\n",
      "0.134577370326\n",
      "0.0381664053319\n",
      "0.0777738347547\n",
      "0.184242610114\n",
      "0.0404269404408\n",
      "0.32980444966\n",
      "0.180963211474\n",
      "0.106486295786\n",
      "0.21232214184\n",
      "0.117517348991\n",
      "0.133149548318\n",
      "0.143726925251\n",
      "0.1906223659\n",
      "0.110399419524\n",
      "0.0757882834354\n",
      "0.115428560184\n",
      "0.245155430087\n",
      "0.201597345681\n",
      "0.0828907747465\n",
      "0.124652127669\n",
      "0.0611311844814\n",
      "0.0421081713917\n",
      "0.331731413034\n",
      "0.0962691926394\n",
      "0.129357081872\n",
      "0.23600878174\n",
      "0.249646239569\n",
      "0.290732305012\n",
      "0.271068691381\n",
      "0.083659866432\n",
      "0.0864680709419\n",
      "0.138283047712\n",
      "0.141143194444\n",
      "0.115577697501\n",
      "0.120652493546\n",
      "0.211948300635\n",
      "0.0950964243756\n",
      "0.0750482480366\n",
      "0.779379149865\n",
      "0.101350099019\n",
      "0.371190844499\n",
      "0.127317514186\n",
      "0.480412315311\n",
      "0.184045555788\n",
      "0.702640208253\n",
      "0.0602974851242\n",
      "0.107688689534\n",
      "0.254496753198\n",
      "0.275545253015\n",
      "0.162177160321\n",
      "0.148663488049\n",
      "0.152927268368\n",
      "0.0847405609913\n",
      "0.577185184692\n",
      "0.482365371356\n",
      "0.302406075616\n",
      "0.114177637707\n",
      "0.834632046929\n",
      "0.447072836831\n",
      "0.453625658229\n",
      "0.303717631896\n",
      "0.388668654708\n",
      "0.0620031262217\n",
      "0.234386738383\n",
      "0.0921746056275\n",
      "0.0767137605567\n",
      "0.166453155066\n",
      "0.121929452846\n",
      "0.0410398813615\n",
      "0.265376589449\n",
      "0.158787012952\n",
      "0.117668268981\n",
      "0.0720376992766\n",
      "0.0927417338259\n",
      "0.368056333638\n",
      "0.272818447544\n",
      "0.195189539376\n",
      "0.0667277376882\n",
      "0.102219932004\n",
      "0.0573147576054\n",
      "0.14203130103\n",
      "0.140695863573\n",
      "0.297086178189\n",
      "0.115692246836\n",
      "0.0896974578841\n",
      "0.212883968871\n",
      "0.537912594253\n",
      "0.13633767771\n",
      "0.605642295646\n",
      "0.0603118862146\n",
      "0.097010259084\n",
      "0.0805877346002\n",
      "0.2808970585\n",
      "0.0971625552899\n",
      "0.162468457898\n",
      "0.171928954109\n",
      "0.558135392405\n",
      "0.222926693887\n",
      "0.0729931014395\n",
      "0.911569404996\n",
      "0.0611906773991\n",
      "0.311793150562\n",
      "0.18676932321\n",
      "0.0759760719554\n",
      "0.757599240202\n",
      "0.510580568758\n",
      "0.0272823498528\n",
      "0.0835841310631\n",
      "0.0947523748896\n",
      "0.730457402513\n",
      "0.398482179361\n",
      "0.572914269385\n",
      "0.0763155257684\n",
      "0.175826040698\n",
      "0.713759393696\n",
      "0.182766576948\n",
      "0.0793871783887\n",
      "0.374786859592\n",
      "0.151331878276\n",
      "0.0646243130823\n",
      "0.527412528369\n",
      "0.159732311836\n",
      "0.0975352379952\n",
      "0.0336933569025\n",
      "0.0878321234817\n",
      "0.11316865221\n",
      "0.483533157701\n",
      "0.159745028832\n",
      "0.424953784781\n",
      "0.33606672128\n",
      "0.0744846487905\n",
      "0.0780150912875\n",
      "0.174399332809\n",
      "0.160432835971\n",
      "0.135267089418\n",
      "0.211093536674\n",
      "0.0591067218279\n",
      "0.199080223171\n",
      "0.0853853255923\n",
      "0.312513695082\n",
      "0.52212156575\n",
      "0.0928910025539\n",
      "0.141341308466\n",
      "0.106794684318\n",
      "0.147737804062\n",
      "0.150757546252\n",
      "0.212100335952\n",
      "0.16262363849\n",
      "0.0550123717167\n",
      "0.0989502885302\n",
      "0.0735681441325\n",
      "0.131387000346\n",
      "0.108897886978\n",
      "0.0825744685977\n",
      "0.540377764497\n",
      "0.2332959671\n",
      "0.0412137483479\n",
      "0.083187224538\n",
      "0.0790882881344\n",
      "0.157152078858\n"
     ]
    }
   ],
   "source": [
    "for item in test_result:\n",
    "    print(item[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
